package com.haofengsoft.scce.ui.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.haofengsoft.platform.companent.imageview.CircleImageView;
import com.haofengsoft.platform.utils.DateUtil;
import com.haofengsoft.platform.utils.StringUtil;
import com.haofengsoft.scce.ForgetPassWordActivity;
import com.haofengsoft.scce.LoginActivity;
import com.haofengsoft.scce.R;
import com.haofengsoft.scce.common.config.ConstantConfig;
import com.haofengsoft.scce.db.bean.UserInfo;
import com.haofengsoft.scce.db.cache.UserCache;
import com.haofengsoft.scce.ui.mine.AboutUsActivity;
import com.haofengsoft.scce.ui.mine.ConsultationCollectActivity;
import com.haofengsoft.scce.ui.mine.FeedBackActivity;
import com.haofengsoft.scce.ui.mine.MinePersonalActivity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

/**
 * 我的
 * 
 * @author zhj
 * 
 */
public class MineFragment extends BaseFragment {

	private TextView loginTv;
	private boolean isLogin = false;
	private UserInfo userInfo;
	private DisplayImageOptions userIconOptions = new DisplayImageOptions.Builder()
			.showImageOnLoading(R.drawable.head_default_icon)
			.showImageForEmptyUri(R.drawable.head_default_icon)
			.showImageOnFail(R.drawable.head_default_icon).cacheInMemory(true)
			.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
			.considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565)
			.build();
	private RelativeLayout noLoginRl, loginRl;
	private RatingBar creditRatingRb;
	private CircleImageView headImageCIv;
	private TextView registDateTv;
	private TextView nickNameTv;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.main_mine, container, false);
		view.setOnTouchListener(new DontSpillOnTouchListener());
		loginTv = (TextView) view.findViewById(R.id.mine_login_tv);
		loginTv.setOnClickListener(new LoginOnClickListener());
		noLoginRl = (RelativeLayout) view.findViewById(R.id.mine_nologin_rl);
		loginRl = (RelativeLayout) view.findViewById(R.id.mine_login_rl);
		creditRatingRb = (RatingBar) view
				.findViewById(R.id.mine_credit_rating_rb);
		nickNameTv = (TextView) view.findViewById(R.id.mine_nick_name_tv);

		registDateTv = (TextView) view.findViewById(R.id.mine_regist_date_tv);
		Button changePassWordBtn = (Button) view
				.findViewById(R.id.mine_changepass_btn);
		changePassWordBtn.setOnClickListener(new ChangePassWordClickListener());
		Button myCollectBtn = (Button) view
				.findViewById(R.id.mine_mycollection_btn);
		myCollectBtn.setOnClickListener(new CollectionOnClickListener());
		Button myPersonalInfoBtn = (Button) view
				.findViewById(R.id.mine_accountinfo_btn);
		myPersonalInfoBtn.setOnClickListener(new PersonalInfoClickListener());
		Button feedbackBtn = (Button) view
				.findViewById(R.id.mine_feedbackinfo_btn);
		feedbackBtn.setOnClickListener(new FeedBackClickListener());
		Button aboutUsBtnButton = (Button) view
				.findViewById(R.id.mine_aboutus_btn);
		aboutUsBtnButton.setOnClickListener(new AboutUsClickListener());
		return view;
	}

	class AboutUsClickListener implements View.OnClickListener {

		@Override
		public void onClick(View view) {

			if (isLogin) {
				Intent aboutUsIntent = new Intent(getActivity(),
						AboutUsActivity.class);
				startActivity(aboutUsIntent);
			} else {
				loginFirst();
			}
		}

	}

	class FeedBackClickListener implements View.OnClickListener {

		@Override
		public void onClick(View view) {
			if (isLogin) {
				Intent feedIntent = new Intent(getActivity(),
						FeedBackActivity.class);

				startActivity(feedIntent);
			} else {
				loginFirst();
			}

		}

	}

	class PersonalInfoClickListener implements OnClickListener {

		@Override
		public void onClick(View view) {
			if (isLogin) {
				Intent userInfoIntent = new Intent(getActivity(),
						MinePersonalActivity.class);
				startActivity(userInfoIntent);
			} else {
				loginFirst();
			}
		}

	}

	class ChangePassWordClickListener implements OnClickListener {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			if (isLogin) {
				Intent intent = new Intent(getActivity(),
						ForgetPassWordActivity.class);
				intent.putExtra("defaultTel", userInfo.getAccount());
				startActivity(intent);
			} else {
				loginFirst();
			}

		}

	}

	@Override
	protected void lazyLoad() {

	}

	class LoginOnClickListener implements View.OnClickListener {

		@Override
		public void onClick(View view) {
			Intent intent = new Intent(getActivity(), LoginActivity.class);
			startActivity(intent);
		}
	}

	class CollectionOnClickListener implements View.OnClickListener {

		@Override
		public void onClick(View view) {
			if (isLogin) {
				Intent intent = new Intent(getActivity(),
						ConsultationCollectActivity.class);
				startActivity(intent);
			} else {
				loginFirst();
			}
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		userInfo = UserCache.getInstance().getUser(getActivity());
		if (-1 == userInfo.getId()) {
			isLogin = false;
			// 没有登录
			noLoginRl.setVisibility(View.VISIBLE);
			loginRl.setVisibility(View.GONE);
		} else {
			isLogin = true;
			// 登录
			noLoginRl.setVisibility(View.GONE);
			loginRl.setVisibility(View.VISIBLE);
			creditRatingRb.setNumStars(userInfo.getCreditRating());
			creditRatingRb.setIsIndicator(true);
			// 状态1=未审核2=待审核3审核通过
			registDateTv
					.setText(StringUtil.isNotBlank(DateUtil
							.getDateByStringDateTime(userInfo.getCreateTime())) ? DateUtil
							.getDateByStringDateTime(userInfo.getCreateTime())
							: "");
			nickNameTv
					.setText(StringUtil.isNotBlank(userInfo.getUserName()) ? userInfo
							.getUserName() : "设置昵称");
		}

		ImageLoader
				.getInstance()
				.displayImage(
						StringUtil.isNotBlank(userInfo.getHeadImageUrl()) ? ConstantConfig.API_URL
								+ userInfo.getHeadImageUrl()
								: "", headImageCIv, userIconOptions);
	}

	private void loginFirst() {
		Toast.makeText(getActivity(), "请先登录", Toast.LENGTH_SHORT).show();
	}

}
